package com.xszx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xszx.service.SuperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.xszx.bean.LoginUser;
import com.xszx.bean.UserVocate;
import com.xszx.bean.WorkTime;

@Controller
@CrossOrigin
public class SuperController {

    @Autowired
    SuperService superService;


    @RequestMapping("/toLoginEmp")
    public String toLoginEmp() {
        return "loginEmp.html";
    }

    @RequestMapping("/toSuperBody")
    public String toSuperBody(Model model) {
        int vacateCount = superService.selectVacateCount();
        model.addAttribute("vacateCount", vacateCount);
        return "superBody.html";
    }

    @RequestMapping("/toAttentionMana")
    public String toAttentionMana() {
        return "attentionMana.html";
    }

    @RequestMapping("/toVacateAgree")
    public String toVacateAgree() {
        return "vacateAgree.html";
    }

    @RequestMapping("/getSuperVacate")
    @ResponseBody
    public Map<String, Object> getSuperVacate(@RequestParam int limit, @RequestParam int page) {
        int offset = (page - 1) * limit;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("offset", offset);
        map.put("limit", limit);
        List<UserVocate> list = superService.selectSuperVacate(map);
        int count = superService.selectVacateCount();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);
        return map;
    }

    @RequestMapping("/agreeVacate")
    @ResponseBody
    public boolean agreeVacate(@RequestBody UserVocate userVacate) {
        System.out.println("请假批准信息：" + userVacate.toString());
        int i = superService.agreeVacate(userVacate);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/getLoginEmp")
    @ResponseBody
    public Map<String, Object> getBody() {
        List<LoginUser> list = superService.selectAllLoginUser();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", list.size());
        map.put("data", list);
        return map;
    }

    @RequestMapping("/del")
    @ResponseBody
    public boolean del(@RequestParam int id) {
        System.out.println(id);
        int i = superService.delALoginUser(id);
        if (i > 0) {
            return true;
        } else {
            return false;
        }

    }

    @RequestMapping("/edit")
    @ResponseBody
    public boolean edit(@RequestBody LoginUser user) {
        System.out.println(user.getAccount() + "," + user.getPassword() + "," + user.getUserName() + "," + user.getPermission());
        //System.out.println(password);
        int i = superService.editALoginUser(user);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/add")
    @ResponseBody
    public boolean add(@RequestBody LoginUser user) {
        System.out.println(user.getAccount() + "," + user.getPassword() + "," + user.getUserName() + "," + user.getPermission());
        int i = superService.addALoginUser(user);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("/editWorkTime")
    @ResponseBody
    public boolean editWorkTime(@RequestBody WorkTime workTime) {
        //System.out.println(workTime.getStart().getHours()+"....."+workTime.getEnd().getHours());
        if (workTime.getStart().getHours() > 10 || workTime.getEnd().getHours() + 12 < 17) {
            return false;
        } else {
            int i = superService.updateWorkTime(workTime);
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        }
    }


//	@RequestMapping("/reLodeAdd")
//	public String reLodeAdd() {
//		return "";
//	}
}
